<?php

namespace Admin\Controller;
use Think\Controller;

class OrderController extends Controller {

    //后台订单列表
    public function order_list(){
        $page = I("post.page");
        $page_size = I("post.page_size");
        $user_id = I('post.user_id');
        if(empty($page)){
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg']  = "缺少当前页数";
            $this->ajaxReturn($ajaxReturn);
        }
        if(empty($page_size)){
            $ajaxReturn['code'] = 3;
            $ajaxReturn['msg'] = "缺少每页展示条数";
            $this->ajaxReturn($ajaxReturn);
        }
        
        $order = M('order');
        $count = $order->count();  //总条数
        $total =  ceil($count / $page_size);  //总页数
        $limitStart = ($page-1) * $page_size; //偏移量
        
        $result = $order->order('id desc')->limit($limitStart,$page_size)->select();
        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

	//广告商订单列表
    public function advertiser_lists(){
    	//$this->isAjaxLogin();	  //检查是否登录
        $name = session('user_info');
        $user_id = $name['id'];
    	$page = I("post.page");
    	$page_size = I("post.page_size");
        //$user_id = I('post.user_id');
        if(empty($page)){
        	$ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "缺少当前页数";
            $this->ajaxReturn($ajaxReturn);
        }
        if(empty($page_size)){
        	$ajaxReturn['code'] = 3;
            $ajaxReturn['msg'] = "缺少每页展示条数";
            $this->ajaxReturn($ajaxReturn);
        }
        
        //$where['ad_id']=$user_id;
        

        //查询当前登录人的订单
        $order = M('order');
        $count = $order->count();  //总条数
        $total =  ceil($count / $page_size);  //总页数
        $limitStart = ($page-1) * $page_size; //偏移量
        
        $result = $order->order('id desc')->limit($limitStart,$page_size)->select();
        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);

    }

    //广告商按条件搜索订单信息
    public function advertiser_search(){
    	//$this->isAjaxLogin();	  //检查是否登录
    	$user_id = I('post.user_id');
        $game_id = I('post.game_id');
    	$page = I('post.page');
    	$page_size = I('post.page_size');
        $start_time = I('post.start_time');
        $end_time = I('post.end_time');
        if(empty($page)){
        	$ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "缺少当前页数";
            $this->ajaxReturn($ajaxReturn);
        }
        if(empty($page_size)){
        	$ajaxReturn['code'] = 3;
            $ajaxReturn['msg'] = "缺少总页数";
            $this->ajaxReturn($ajaxReturn);
        }

        $where='1';
        if($user_id){
            $where.=" and ch_id=$user_id";
        }
        if($game_id){
            $where.=" and game_id=$game_id";
        }
        if($start_time){
            $where.=" and notify_downsteam_time>=$start_time";
        }
        if($end_time){
            $where.=" and notify_downsteam_time<=$end_time";
        }
        $order = M('order');
        $count = $order->where($where)->count();  //总条数
        $total =  ceil($count / $page_size);

        $limitStart = ($page-1) * $page_size;
        $result = $order->order("id desc")->where($where)->limit($limitStart,$page_size)->select();
        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            // $ajaxReturn['code'] = 200;
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);

    }

    //渠道商订单列表
    public function channel_lists(){
    	//$this->isAjaxLogin();	  //检查是否登录
    	$page = I("post.page");
    	$page_size = I("post.page_size");
        
        $order = M('order');
        $count = $order->where($where)->count();  //总条数
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        
        $result = $order->order("id desc")->limit($limitStart,$page_size)->select();
        if($result !== false){
 
            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    //渠道商按条件查找订单列表
    public function channel_search(){
    	//$this->isAjaxLogin();	  //检查是否登录
    	$page = I('post.page');
        $userid = I('post.user_id');
    	$page_size = I('post.page_size');
        $game_id = I('post.game_id');
        $start_time = I('post.start_time');
        $end_time = I('post.end_time');
        $where = 1;
        if($userid){
            $where.=" and ch_id=$userid";
        }
        if($game_id){
            $where.=" and game_id=$game_id";
        }
        if($start_time){
            $where.=" and notify_downsteam_time>=$start_time";
        }
        if($end_time){
            $where.=" and notify_downsteam_time<=$end_time";
        }

        $order = M('order');
        $count = $order->where($where)->count();  //总条数
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        
        $result = $order->order("id desc")->where($where)->limit($limitStart,$page_size)->select();
         
        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    //广告主列表
    public function advertisers(){
        $data = M('channel')->field("contacts_name,id,corporate_name")->where("status=1")->select();

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        

        $this->ajaxReturn($ajaxReturn);
    }


    //获取游戏列表
    public function game_name(){

        $id = I('get.user_id');

        $where=1;

        if($id){
            $where.=" and ad_id=$id";
            
        }
        $where.=" and status=1";
        $data = M('game_advertisers')->field("game_name,id")->where($where)->select();

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";
        $ajaxReturn['data'] = $data;
       

        $this->ajaxReturn($ajaxReturn);
    }

}
